<link href="<?=APP_STATIC_PATH?>/admin/css/bootstrap.min.css" rel="stylesheet">
<link href="<?=APP_STATIC_PATH?>/admin/css/bootstrap-theme.min.css" rel="stylesheet">
<link href="<?=APP_STATIC_PATH?>/admin/ext/font-awesome/css/font-awesome.min.css" rel="stylesheet">
<script src="<?=APP_STATIC_PATH?>/admin/js/jquery.min.js"></script>
<script src="<?=APP_STATIC_PATH?>/admin/js/bootstrap.min.js"></script>
<link href="<?=APP_STATIC_PATH?>/admin/ext/metisMenu/metisMenu.css" rel="stylesheet">
<script src="<?=APP_STATIC_PATH?>/admin/ext/metisMenu/metisMenu.js"></script>
<link href="<?=APP_STATIC_PATH?>/admin/ext/dataTables/jquery.dataTables.min.css" rel="stylesheet">
<script src="<?=APP_STATIC_PATH?>/admin/ext/dataTables/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="<?=APP_STATIC_PATH?>/admin/css/jquery.datetimepicker.css" type="text/css">
<script src="<?=APP_STATIC_PATH?>/admin/ext/dataTables/dataTables.tableTools.min.js" charset="utf-8"></script>
<link href="<?=APP_STATIC_PATH?>/admin/ext/dataTables/dataTables.tableTools.min.css" rel="stylesheet">
<script src="<?=APP_STATIC_PATH?>/admin/js/jquery.datetimepicker.js"></script>

<link href="<?=APP_STATIC_PATH?>/admin/css/cfg_admin.css" rel="stylesheet">
<link href="<?=APP_STATIC_PATH?>/admin/css/cfg_table.css" rel="stylesheet">

<script>
    /*
     * 自动提交表单
     * 需要指定父元素class为 form
     * 需要指定提交按钮的class为 submit
     * 需要提交的字段，节点给一个name属性，这个属性就是提交到后台的字段名字
     * 必要的字段，添加notnull属性，提交的时候，如果带有该属性的字段没有填写，则会提示
     * 可在提交按钮上 添加一个success='callback'的属性，当ajax成功时，会去全局范围调用callback并回传ajax返回的data
     */
    $(document).on('click', '.submit', function(){

        var me = this;
        var $el = $(this);
        var $par = $el.closest('form').length ? $el.closest('form') : $('body');
        var $val = $par.find('[post-name]');
        var $check = $par.find('[rule]');
        var $nece = $par.find('[notnull]');
        var data = BEE.analyseData($el, 'post');
        var url = data.url;
        var err = false;
        var $errTar = null;
        //验证 是否为空
        $.each($nece, function(k, v){
            if(!$.trim($(v).val())){
                $errTar = $(v);
                err = true;
                return false;
            }
        });
        if(err){
             BEE.dialog("此项不能为空");
             $errTar.focus();
             return false;
        }
        //验证
        $.each($check, function(k, v){
            rule = $(v).attr('rule');
            val = $(v).val();
            error = $(v).attr('error');
            ret = check(rule, val, error);
            if(!ret){
                err = true;
                return false;
            }
        });
        if(err){
            return false;
        }

        //取得所有input val
        $.each($val, function(k, v){
            data[$(v).attr('post-name')] = $(v).val();
        });
        delete data.url;
        //自定义处理data的函数
        var beforePost = $el.attr('beforePost');
        if(typeof window[beforePost] === 'function'){
            data = window[beforePost](data, $par);
        }

        if(this.ajaxing){
            return false;
        }
        this.ajaxing = true;
        this.ajax = $.ajax({
            url: url,
            data: data,
            type: 'post',
            dataType: 'json'
        });
        //成功
        this.ajax.then(function(data){
            //如果按钮上有success函数
            if(typeof(data) == "object"){
                if(data.retcode == 2000000){
                    var success = $el.attr('success');
                    if(typeof window[success] === 'function'){
                        window[success](data, 'success');
                    }
                    else{
                        BEE.dialog(data.msg);
                    }
                }else{
                    var fail = $el.attr('fail');
                    if(typeof window[fail] === 'function'){
                        window[fail](data, 'fail');
                    }
                    else{
                        BEE.dialog(data.msg);
                    }
                }
            }else{
                BEE.dialog('请求失败');
            }
        });
        //失败
        this.ajax.fail(function(data){
            //如果按钮上有fail函数
            var afterPost = $el.attr('afterPost');
            if(typeof window[afterPost] === 'function'){
                window[afterPost](data, 'fail');
            }
            else{
                BEE.dialog('请求失败');
            }
        });
        //结束
        this.ajax.complete(function(){
            me.ajaxing = false;
        });
    });

    var BEE = {
        /* dialog对话框
         * @param html(string)              窗体内容
         * @param title(string)             窗体标题
         * @param contentClass(string)      窗体class
         * @param width(num)                窗体宽
         * @param time(num)                 窗体自动关闭时间
         * @param closeCallback(fn)         窗体关闭后的回调
         */
        dialog: function(cfg){
            function Dialog(cfg){
                var me = this;
                var _default = {
                    html: '谁还没有个默认值呀~',
                    title: '提示',
                    contentClass: '',
                    width: 300,
                    time: null,
                    closeCallback: null,
                    auto: null
                };
                //直接传入字符串
                if(cfg.constructor === String){
                    var temp = cfg;
                    cfg = {};
                    cfg.html = temp;
                }

                this.opt = $.extend({}, _default, cfg);
                this.size = {
                    x: $(document).width(),
                    y: Math.max($(document).height(), $(window).height()),
                    scrollTop: $(document).scrollTop()
                };
                this.open();
            };

            Dialog.prototype.open = function(){
                var self = this,
                    opt = this.opt,
                    size = this.size,
                    mask = $('<div></div>'),
                    content = $('<div class='+opt.contentClass+'>\
                            <div class="dialog-title">'+opt.title+'<a class="dialog-close" href="javascript:">×</a></div>\
                            <div class="dialog-content">'+opt.html+'</div>\
                        </div>');
                this.mask = mask;
                this.content = content;
                mask.css({
                    'width': '100%',
                    'height': size.y + 'px',
                    'position': 'absolute',
                    'left': 0,
                    'top': 0,
                    'z-index': 10000,
                    'background': '#000',
                    'opacity': '0.2',
                    'filter': 'alpha(opacity=20)'
                });
                content.css({
                    'position': 'fixed',
                    'width': opt.width + 'px',
                    'left': '-9999px',
                    'top': '-9999px',
                    'background': '#fefefe',
                    'border-radius': '4px',
                    'box-shadow': '0 0 10px #555',
                    'text-align': 'center',
                    'z-index': 10010
                });
                content.find('.dialog-title').css({
                    'background': '#f1f1f1',
                    'border-radius': '4px 4px 0 0',
                    'border-bottom': '1px solid #fff',
                    'height': '40px',
                    'line-height': '32px',
                    'font-weight': 'bold',
                    'text-align': 'left',
                    'padding-left': '15px'
                });
                content.find('.dialog-close').css({
                    'position': 'absolute',
                    'top': '1px',
                    'right': '15px',
                    'font-size': '18px'
                }).click(function(){
                    self.close();
                    opt.closeCallback && opt.closeCallback();
                });
                content.find('.dialog-content').css({
                    'padding': '10px 0'
                });
                mask.appendTo($('body'));
                content.appendTo($('body'));

                var tarPos = {
                    'top': ($(window).height() - content.height())/2,
                    'left': (size.x - opt.width)/2
                };
                content.css({
                    'top': tarPos.top - 50 + 'px',
                    'left': tarPos.left + 'px',
                    'opacity': 0,
                    'filter': 'alpha(opacity=0)'
                }).animate({
                    'top': tarPos.top + 'px',
                    'opacity': 1,
                    'filter': 'alpha(opacity=100)'
                }, 300);
                //自动关闭
                if(opt.time){
                    opt.auto = setTimeout(function(){
                        self.close();
                        opt.closeCallback && opt.closeCallback();
                    }, opt.time);
                }
            };

            Dialog.prototype.close = function(){
                var me = this,
                    opt = this.opt,
                    mask = this.mask,
                    content = this.content;
                mask.fadeOut(100, function(){
                    mask.remove();
                    content.remove();
                    me.globalDialog = null;
                    opt.closeCallback && opt.closeCallback();
                });
            };

            return this.globalDialog = new Dialog(cfg);
        },
        analyseData: function($obj, key){
            if(!$obj || !$obj.length || !$obj.attr(key)){
                console.log("请检查数据解析函数传入参数是否正确。");
                return;
            }
            var attr = "url="+$obj.attr(key);
            var arr = attr.split("&");
            var len = arr.length;
            var data = {};
            for(var i=0;i<len;i++){
                var _arr = arr[i].split("=");
                data[_arr[0]] = _arr[1];
            }
            return data;
        }
    };

    function check(rule, value, error){
        switch(rule){
            case 'phone':
                reg = /^1[3456789][0-9]{9}$/;
                if(!reg.test(value)){
                    alert("手机号码格式不正确");
                    return false;
                }
                break;
            case 'mail':
                reg = /^[\w.-]+@[\w.-]*$/;
                if(!reg.test(value)){
                    alert("邮箱地址格式不正确");
                    return false;
                }
                break;
            case 'digit':
                reg = /^[\d]+$/;
                if(!reg.test(value)){
                    alert(error);
                    return false;
                }
                break;
        }
        return true;
    }

    //Loads the correct sidebar on window load,
    //collapses the sidebar on window resize.
    $(function() {
        $(window).bind("load resize", function() {
            width = (this.window.innerWidth > 0) ? this.window.innerWidth : this.screen.width;
            if (width < 768) {
                $('div.sidebar-collapse').addClass('collapse');
                $("#side-menu-icon").show();
                $($("#side-menu > li")[0]).hide();
            } else {
                $('div.sidebar-collapse').removeClass('collapse');
                $("#side-menu-icon").hide();
                $($("#side-menu > li")[0]).show();
            }
        });
        $('#side-menu').metisMenu();
    });

    // 生成弹窗 如果只传第一个参数必须是url地址
    function openDialog(id, url) {
        if(arguments.length === 1) { // 兼容优化处理，仅需要传入url地址即可
            url = id;
            id = 'Modal';
        }
        if($('#'+id).size() < 1) {
            var dialogHtml = '<div class="modal fade" id="'+id+'" tabindex="-1" role="dialog" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"></div></div></div>';
            $('body').append(dialogHtml);
        }

        $.get(url,function(data){
            if(data.retcode && data.retcode != 2000000){
                alert(data.msg.replace(/<.*>/,''));
            }else {
                $('#' + id).modal({remote: url}).on('hidden.bs.modal', function (e) {
                    $(this).removeData("bs.modal");
                });
            }
        });
    }

    // 刷新当前页
    function refresh() {
        window.location.reload();
    }

    // 跳转指定页面
    function jumpUrl(url) {
        window.location.href=url;
    }

    //空验证
    function isNull2(col, id) {
        var value = document.getElementById(id).value
        if (value == "" || value == null) {

            //console.log(tip);
            var tip = id.substring(0, 2) + "tip";
            $('#' + tip).html("请填写" + col);
            $('#' + id).focus();
            return 1;
        } else {
            return 0;
        }
    }

    //js获取指定的cookie的值
    function getCookie(name) {
        var cookieArray = document.cookie.split("; "); //得到分割的cookie名值对

        for (var i = 0; i < cookieArray.length; i++) {
            var arr = cookieArray[i].split("=");       //将名和值分开
            if (arr[0] == name)return unescape(arr[1]); //如果是指定的cookie，则返回它的值
        }
        return "";
    }

    //设置cookie
    function setCookie(name, value, hours, path, domain, secure) {
        var cdata = name + "=" + value;
        if (hours) {
            var d = new Date();
            d.setHours(d.getHours() + hours);
            cdata += "; expires=" + d.toGMTString();
        }
        cdata += path ? ("; path=" + path) : "";
        cdata += domain ? ("; domain=" + domain) : "";
        cdata += secure ? ("; secure=" + secure) : "";
        document.cookie = cdata;
    }

    // 清除cookie
    function clearCookie() {
        var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
        if (keys) {
            for (var i = keys.length; i--;)
                document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
        }
    }

    //删除指定名称的cookie
    function delCookie(name) {
        document.cookie = name + '=0;expires=' + new Date(0).toUTCString()
    }

    //查找数组
    function inArray(arr, val) {
        var ret = false, i = 0, len = arr.length;
        for (; i < len; i++) {
            if (arr[i] === val) { // 强类型校验
                ret = true;
                break;
            }
        }
        return ret;
    }

    function selectAll(name) { // 全选框
        var i = 1;
        $(name).change(function () {
            if (i % 2 == 1) $("input[name='check']").prop("checked", true);
            if (i % 2 == 0) $("input[name='check']").prop("checked", false);
            i++;
        });
    }

    function formatMoney(name) {
        $(name).keyup(function () {
            //如果输入非数字，则替换为''，如果输入数字，则在每4位,分隔
            this.value = this.value.replace(/[,]/g, '').replace(/(\d)(?=(\d{4})+(?!\d))/g, "$1,");
        });
    }

    function decimal(name) {
        $(name).blur(function () {
            //保留两位小数
            var f = parseFloat(this.value);
            if (isNaN(f)) {     //检查空
                return false;
            }
            var f = Math.round(this.value * 100) / 100;  //如有2位以上小数，则截取
            var s = f.toString();
            var rs = s.indexOf('.');    //判断有无小数点
            if (rs < 0) {
                rs = s.length;
                s += '.';           //补充小数点
            }
            while (s.length <= rs + 2) {
                s += '0';          //补充0
            }
            this.value = s;
        });
    }

    if(typeof (TableTools) != "undefined"){
        TableTools.DEFAULTS.aButtons = ["csv"];
    }
    $.extend( $.fn.dataTable.defaults, {
        initComplete: function () {
            var api = this.api();
            api.columns().eq(0).each(function(colIdx) {
                th = api.columns(colIdx).header().toJQuery();
                if(th.hasClass("add_search")){
                    var column = api.column(colIdx);
                    title = th.text();
                    $('<input type="text" class="search_input inline" placeholder="搜索'+title+
                        '" style="width: 100px;margin-left: 8px">')
                        .appendTo('#search_area')
                        .on('keyup change', function () {
                            var val = $.fn.dataTable.util.escapeRegex($(this).val());
                            column.search(val).draw();
                        });
                }
            });
        },
        "dom": '<"top">rt<"bottom"p><"clear">',
        "iDisplayLength": 10,
        "deferRender": true,
        "processing": true,
        "lengthMenu": [10, 100, 1000, 10000],
        "language": {
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "抱歉， 没有找到",
            "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
            "sInfoEmpty": "没有数据",
            "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
            }
        }
    });
</script>
